package com.travel.travel_springboot_web.mapper;

import com.travel.travel_springboot_web.entity.Cart;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CartMapper {
    @Insert("INSERT INTO cart (user_id, goods_id, num, gift_num, selected, created_at, updated_at) " +
            "VALUES (#{userId}, #{goodsId}, #{num}, #{giftNum}, #{selected}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Cart cart);

    @Select("SELECT c.*, g.title as goods_title, g.cover_image as goods_image, " +
            "g.price as goods_price, g.stock as goods_stock " +
            "FROM cart c " +
            "LEFT JOIN goods g ON c.goods_id = g.id " +
            "WHERE c.user_id = #{userId} " +
            "ORDER BY c.created_at DESC")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "goodsId", column = "goods_id"),
        @Result(property = "goodsTitle", column = "goods_title"),
        @Result(property = "goodsImage", column = "goods_image"),
        @Result(property = "goodsPrice", column = "goods_price"),
        @Result(property = "goodsStock", column = "goods_stock"),
        @Result(property = "createdAt", column = "created_at"),
        @Result(property = "updatedAt", column = "updated_at"),
        @Result(property = "giftNum", column = "gift_num")
    })
    List<Cart> findByUserId(Long userId);

    @Update("UPDATE cart SET num = #{num}, gift_num = #{giftNum}, updated_at = NOW() " +
            "WHERE id = #{id} AND user_id = #{userId}")
    int updateNum(@Param("id") Long id, @Param("userId") Long userId, 
                 @Param("num") Integer num, @Param("giftNum") Integer giftNum);

    @Update("UPDATE cart SET selected = #{selected}, updated_at = NOW() " +
            "WHERE id = #{id} AND user_id = #{userId}")
    int updateSelected(@Param("id") Long id, @Param("userId") Long userId, @Param("selected") Integer selected);

    @Delete("DELETE FROM cart WHERE id = #{id} AND user_id = #{userId}")
    int delete(@Param("id") Long id, @Param("userId") Long userId);

    @Select("SELECT COUNT(*) FROM cart WHERE user_id = #{userId} AND goods_id = #{goodsId}")
    int checkExists(@Param("userId") Long userId, @Param("goodsId") Long goodsId);

    @Update("UPDATE cart SET num = num + #{num}, gift_num = gift_num + #{giftNum}, " +
            "updated_at = NOW() WHERE user_id = #{userId} AND goods_id = #{goodsId}")
    int updateNumByGoodsId(@Param("userId") Long userId, @Param("goodsId") Long goodsId, 
                          @Param("num") Integer num, @Param("giftNum") Integer giftNum);

    @Select("SELECT num FROM cart WHERE user_id = #{userId} AND goods_id = #{goodsId}")
    Integer getCurrentNum(@Param("userId") Long userId, @Param("goodsId") Long goodsId);

    @Select("SELECT * FROM cart WHERE id = #{id}")
    @Results({
        @Result(property = "userId", column = "user_id"),
        @Result(property = "goodsId", column = "goods_id"),
        @Result(property = "createdAt", column = "created_at"),
        @Result(property = "updatedAt", column = "updated_at")
    })
    Cart findById(Long id);

    @Select("SELECT * FROM cart WHERE user_id = #{userId} AND goods_id = #{goodsId}")
    Cart findByUserIdAndGoodsId(@Param("userId") Long userId, @Param("goodsId") Long goodsId);
    
    @Update("UPDATE cart SET num = #{num}, gift_num = #{giftNum}, updated_at = NOW() " +
            "WHERE id = #{id} AND user_id = #{userId}")
    int updateNumAndGiftNum(@Param("id") Long id, 
                           @Param("userId") Long userId, 
                           @Param("num") Integer num, 
                           @Param("giftNum") Integer giftNum);
} 